import { Object } from "core-js";
import { saveOrUpdateData } from "@/api/wisdom-beam/beamInfoConfig";
export default {
  data() {
    return {
      form: {
        //梁号
        componentParts: "",
        //所属桥梁
        bridgeNameSource: [],
        bridgeName: undefined,
        //联
        unite: "",
        //跨
        span: "",
        //幅别
        amClass: undefined,
        //长度
        beamLength: "",
        //梁类型
        beamTypeSource: [],
        beamType: undefined,
        //强度等级
        strengthGradeSource: [],
        strengthGrade: undefined,
        //设计方量
        designVolume: "",
        //坍落度
        slump: "",
        //水泥
        cement: "",
        //细骨料
        naturalSand: "",
        //粉煤灰
        flyAsh: "",
        //水
        water: "",
        //粗骨料(5-10)
        zhongshizi: "",
        //粗骨料(10-20)
        dashizi: "",
        //外加剂
        waterReducingAgent: "",
        //钢筋信息
        rebarModeSource: [],
        breedSource: [],
        diameterSource: [],
        //钢筋信息
        lcBeamGjInfoList: [],
        //张拉钢筋信息
        zlBeamGjInfo: {},
      },
      rules: {
        componentParts: [
          { required: true, message: "请输入梁号", trigger: "blur" },
        ],
        unite: [{ required: true, message: "请输入联", trigger: "blur" }],
        span: [{ required: true, message: "请输入span", trigger: "blur" }],
        bridgeName: [
          { required: true, message: "请输入所属桥梁", trigger: "change" },
        ],
        amClass: [{ required: true, message: "请选择幅别", trigger: "change" }],
        beamLength: [
          { required: true, message: "请输入长度", trigger: "blur" },
        ],
        beamType: [
          { required: true, message: "请选择梁类型", trigger: "change" },
        ],
      },
      span_1: {
        xs: 24,
        sm: 24,
        md: 24,
        lg: 12,
        xl: 6,
        xxl: 6,
      },
      span_2: {
        xs: 24,
        sm: 24,
        md: 24,
        lg: 12,
        xl: 18,
        xxl: 18,
      },
      span_3: {
        xs: 24,
        sm: 24,
        md: 24,
        lg: 12,
        xl: 12,
        xxl: 6,
      },
      span_4: {
        xs: 24,
        sm: 24,
        md: 24,
        lg: 12,
        xl: 12,
        xxl: 8,
      },
    };
  },
  mounted() {},
  methods: {
    //确定
    onSubmit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.submitLoading = true;
          let keys = [
            "flyAsh",
            "water",
            "zhongshizi",
            "dashizi",
            "waterReducingAgent",
            "naturalSand",
            "bridgeName",
            "componentParts",
            "amClass",
            "beamLength",
            "unite",
            "span",
            "beamType",
            "strengthGrade",
            "designVolume",
            "slump",
            "cement",
          ];
          let { form, row } = this;
          let { id } = row;
          let { lcBeamGjInfoList, zlBeamGjInfo } = form;
          let params = { lcBeamGjInfoList: [], zlBeamGjInfo: {} };
          Object.keys(form).forEach((key) => {
            if (keys.includes(key)) {
              params[key] = form[key];
            }
          });
          if (id) {
            params.id = id;
          }
          //钢筋信息
          lcBeamGjInfoList.forEach((v) => {
            let { model, gjNumber, rebarDiameter, rebarType } = v;
            params.lcBeamGjInfoList.push({
              model,
              rebarDiameter,
              rebarType,
              gjNumber,
            });
          });
          //张拉钢筋信息
          params.zlBeamGjInfo = {
            model: zlBeamGjInfo.model,
            rebarDiameter: zlBeamGjInfo.rebarDiameter,
            rebarType: zlBeamGjInfo.rebarType,
            gjNumber: zlBeamGjInfo.gjNumber,
          };
          saveOrUpdateData(params)
            .then((res) => {
              this.$parent.getData();
              this.goBack();
            })
            .finally((_) => {
              this.submitLoading = false;
            });
        } else {
          return false;
        }
      });
    },

    //校验表单元素的禁用
    checkFormDisabled() {
      let { form } = this;
      let { lcBeamGjInfoList } = form;
      Object.keys(form).forEach((key) => {
        if (key != "lcBeamGjInfoList") {
          this.setDisabled(form, key);
        } else {
          lcBeamGjInfoList.forEach((v) => {
            Object.keys(v).forEach((rkey) => {
              this.setDisabled(v, rkey);
            });
          });
        }
      });
    },

    setDisabled(target, key) {
      let { type, row, rules } = this;
      let { editAll, editPart } = row;
      let requiredKeys = Object.keys(rules);
      let val = true;
      let operate = Number(type);
      //新增
      if (operate == 1) {
        val = false;
      }
      //查看
      if (operate == 2) {
        val = true;
      }

      //编辑
      if (operate == 3) {
        if (editAll) {
          //编辑全部
          val = false;
        } else if (editPart) {
          //编辑必填项
          if (requiredKeys.includes(key)) {
            val = true;
          } else {
            val = false;
          }
        } else {
          //无编辑权限
          val = false;
        }
      }

      target[key + "Disable"] = val;
    },

    //验证一联一跨
    validateUnionspan(rule, value, callback) {
      let { unite, span, uniteDisable, spanDisable } = this.form;
      if (!unite && !uniteDisable) {
        callback(new Error(`请输入联`));
      } else if (!span && !spanDisable) {
        callback(new Error(`请输入跨`));
      } else {
        callback();
      }
    },

    //新增钢筋信息
    addRebar() {
      let baseJson = {
        //型号
        model: undefined,
        //直径
        rebarDiameter: undefined,
        //品种
        rebarType: undefined,
        //数量
        gjNumber: "",
      };
      this.form.lcBeamGjInfoList.push(baseJson);
    },

    //删除钢筋信息
    delRebar(index) {
      this.form.lcBeamGjInfoList = this.form.lcBeamGjInfoList.filter(
        (_, i) => i != index
      );
    },
  },
};
